#include "mainwindow.h"
#include "ui_mainwindow.h"

MainWindow::MainWindow(QWidget *parent)
    : QMainWindow(parent)
    , ui(new Ui::MainWindow)
{
    ui->setupUi(this);
}

MainWindow::~MainWindow()
{
    delete ui;
}
void MainWindow::mousePressEvent(QMouseEvent *event)
{
    if(event->button() == Qt::LeftButton){
        lastpt = event->pos();
        if(isLightDrawing){
            m_Lpath.addEllipse(lastpt,25,25);
            m_Lpath.moveTo(lastpt);
            m_Lpath.lineTo(lastpt.x()-15,lastpt.y()-20);
            m_Lpath.moveTo(lastpt);
            m_Lpath.lineTo(lastpt.x()-15,lastpt.y()+20);
            m_Lpath.moveTo(lastpt);
            m_Lpath.lineTo(lastpt.x()+15,lastpt.y()+20);
            m_Lpath.moveTo(lastpt);
            m_Lpath.lineTo(lastpt.x()+15,lastpt.y()-20);
        }
        if(isOn_OffDrawing){
            QPointF tempt(lastpt.x()-10,lastpt.y()-10);
            m_On_Off.moveTo(tempt);
            m_On_Off.lineTo(lastpt.x()-10,lastpt.y()+20);//
            tempt.setX(lastpt.x()+10);
            tempt.setY(lastpt.y()-15);
            m_On_Off.moveTo(tempt);
            m_On_Off.lineTo(lastpt.x()+10,lastpt.y()+30);//
            m_On_Off.moveTo(lastpt.x()-10,lastpt.y());
            m_On_Off.lineTo(lastpt.x()-25,lastpt.y());//
            m_On_Off.moveTo(lastpt.x()+10,lastpt.y());
            m_On_Off.lineTo(lastpt.x()+25,lastpt.y());//
        }
        if(isRDrawing){
            m_R.addRect(lastpt.x()-22.5,lastpt.y()-7.5,45,15);
        }
        if(isVDrawing){
            m_V.addEllipse(lastpt,25,25);
            m_V.moveTo(lastpt.rx(),lastpt.y()+25);
            m_V.lineTo(lastpt.x()-15,lastpt.y()-20);
            m_V.moveTo(lastpt.rx(),lastpt.y()+25);
            m_V.lineTo(lastpt.x()+15,lastpt.y()-20);
            m_V.moveTo(lastpt.x()-14.5,lastpt.y());
            m_V.lineTo(lastpt.x()+14.5,lastpt.y());
    }

        isDrawing = true;
    }
}
void MainWindow::mouseMoveEvent(QMouseEvent *event)
{
    if(event->buttons() & Qt::LeftButton){
        curpt = event->pos();
        update();
    }
}
void MainWindow::mouseReleaseEvent(QMouseEvent *event)
{
    if(event->button() == Qt::LeftButton){
        curpt = event->pos();
        if(isLineDrawing){
            lines.append(myline.MakeLine());
        }
        isDrawing = false;
        update();
    }
}
void MainWindow::paintEvent(QPaintEvent *)
{
    QPainter paint(this);
    //----------------线操作----------------
    if(isLineDrawing){
    myline.SetLastPosition(lastpt);
    myline.SetCurPossition(curpt);
    paint.drawLine(myline.MakeLine());
    }
    paint.drawLines(lines);
    //-------------------------------------
    paint.drawPath(m_Lpath);//灯
    paint.drawPath(m_On_Off);//开关
    paint.drawPath(m_R);//电阻
    paint.drawPath(m_V);//电压表
}

void MainWindow::on_pushButton_clicked()
{//线
    isLineDrawing = true;
    isLightDrawing = false;
    isOn_OffDrawing = false;
    isRDrawing = false;
    isVDrawing = false;
}

void MainWindow::on_pushButton_2_clicked()
{
    isLineDrawing = false;
}

void MainWindow::on_pushButton_3_clicked()
{//灯
    isLightDrawing = true;
    isLineDrawing = false;
    isOn_OffDrawing = false;
    isRDrawing = false;
    isVDrawing = false;
}

void MainWindow::on_pushButton_4_clicked()
{//开关
    isOn_OffDrawing = true;
    isLightDrawing = false;
    isLineDrawing = false;
    isRDrawing = false;
    isVDrawing = false;
}

void MainWindow::on_clear_clicked()
{
    isLineDrawing = false;
    isLightDrawing = false;
    isOn_OffDrawing = false;
    isRDrawing = false;
    isVDrawing = false;
    lines.clear();
    m_On_Off.clear();
    m_Lpath.clear();
    m_R.clear();
    m_V.clear();
}

void MainWindow::on_RItem_clicked()
{//电阻
    isRDrawing = true;
    isOn_OffDrawing = false;
    isLightDrawing = false;
    isLineDrawing = false;
    isVDrawing = false;
}

void MainWindow::on_VItem_clicked()
{//电压表
    isVDrawing = true;
    isRDrawing = false;
    isOn_OffDrawing = false;
    isLightDrawing = false;
    isLineDrawing = false;
}
